package com.example.administrator.timertask;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.example.administrator.timertask.adapter.MarqueeAdapter;
import com.example.administrator.timertask.bean.ProfitBean;

import java.util.ArrayList;
import java.util.List;

public class HorizontalScrollViewActivity extends AppCompatActivity {
    private HorizontalScrollView mNoticeScrollView;
    private TranslateAnimation mRigthToLeftAnim;
    private final static float SCOLL_V = 0.2f;
    private RelativeLayout ll_marquee;
    private MarqueeAdapter marqueeAdapter;
    private List<ProfitBean> mList;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_horizontalscrollview);
        initView();
        initAdapter();
    }

    private void initAdapter() {
        mList = new ArrayList<>();
        for(int i=0;i<8;i++){
            ProfitBean profitBean = new ProfitBean();
            profitBean.setName("恭喜张昊天战队爱尚值收益222222.88元");
            mList.add(profitBean);
        }
        marqueeAdapter = new MarqueeAdapter(this,mList);
        recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
        recyclerView.setAdapter(marqueeAdapter);
    }

    private void initView() {
        ll_marquee = findViewById(R.id.ll_marquee);

        mNoticeScrollView = (HorizontalScrollView) findViewById(R.id.horiSv);
         recyclerView = findViewById(R.id.rv_marquee);


        recyclerView.post(new Runnable() {
            @Override
            public void run() {
                mRigthToLeftAnim = new TranslateAnimation(mNoticeScrollView.getWidth(), -recyclerView.getWidth(), 0, 0);
                mRigthToLeftAnim.setRepeatCount(Animation.INFINITE);
                mRigthToLeftAnim.setInterpolator(new LinearInterpolator());
                mRigthToLeftAnim.setDuration((long) ((mNoticeScrollView.getWidth() + recyclerView.getWidth()) / SCOLL_V));
                recyclerView.startAnimation(mRigthToLeftAnim);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        recyclerView.clearAnimation();
    }
}
